package com.bcu.calculus.controller;

import com.bcu.calculus.config.GlobalConfig;
import com.bcu.calculus.entity.Image;
import com.bcu.calculus.entity.Message;
import com.bcu.calculus.service.ImageService;
import com.bcu.calculus.utils.LoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Api(tags = "04  图片模块")
@RestController
@RequestMapping("/api/Image")
public class ImageController {


        @Resource
        private ImageService imageService;

        @Resource
        private GlobalConfig globalConfig;

        @ApiOperation(value="上传图片")
        @PostMapping(value="/Images" , headers = "content-type=multipart/form-data")
        public String getImages(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
                return globalConfig.hostUrl+"bcucalculusFile/image/userSendImage/"+ LoadUtil.upload(globalConfig.filePath+"image/userSendImage/",files, request);
        }

        @ApiOperation(value="上传图片到某个目录下")
        @PostMapping(value="/ImagesToFile" , headers = "content-type=multipart/form-data",produces = "text/plain;charset=utf-8")
        public String getImage(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request,String SemId,String routerId,String userId,String actId) throws IOException {

                String file = SemId+"/"+routerId+"/"+userId+"/"+actId; //目录
                String path = globalConfig.filePath+"image/userSendImage/"+file;
                System.out.print(path);
                File dir = new File(path);
                if(!dir.exists()) {
                        /*
                         * mkdirs方法在创建当前目录时，会自动创建所有不存在的父目录
                         */
                        dir.mkdirs();
                        System.out.println("创建目录成功");
                }


                return globalConfig.hostUrl+"bcucalculusFile/image/userSendImage/"+file+"/"+LoadUtil.upload(globalConfig.filePath+"image/userSendImage/"+file+"/",files, request);
        }

        @ApiOperation(value = "添加图片")
        @PostMapping(value = "/inster")
        public Message<?> insert(Image image){
            return imageService.insert(image);
        }


        @ApiOperation(value = "通过关键字删除")
        @DeleteMapping(value = "/deleteByPrimaryKey")
        public Message deleteByPrimaryKey(Integer imageId) {
            return imageService.deleteByPrimaryKey(imageId);
        }


        @ApiOperation(value = "通过主键修改")
        @PutMapping(value = "/updateByPrimaryKey")
        public Message updateByPrimaryKey(Image image){
            return imageService.updateByPrimaryKey(image);
        }

        @ApiOperation(value = "通过关键字查询")
        @GetMapping(value = "/selectByPrimaryKey")
        public Message<Image>selectByPrimaryKey(Integer imageId) {
            return imageService.selectByPrimaryKey(imageId);
        }

        @ApiOperation(value = "查询所有图片")
        @GetMapping(value = "/selectAllAct")
        public Message<List<Image>>selectAllAct(){
            return imageService.selectAllImage();
        }


}

